package dslab.util;

import java.util.List;
import java.util.Map;

/**
 * class that stores all data necessary for the task of removing a message, which will
 * be executed in a concurrency safe manner at the next opportunity
 *
 * stores message id to be removed, as well as user (name)
 */
public class RemoveMessageTask implements WriteTask {
    private final String componentId;
    private final int id;
    private final String user;
    private final Map<String, List<Message>> mails;

    /**
     * constructs an RemoveMessageTask object
     * @param componentId name of component
     * @param id id number to be removed, represents implicit message position in list
     * @param user user name to remove message for
     * @param mails reference to store of all mails
     */
    public RemoveMessageTask(String componentId, int id, String user, Map<String, List<Message>> mails) {
        // inherit ids, user and mail store reference
        this.componentId = componentId;
        this.id = id;
        this.user = user;
        this.mails = mails;
    }

    @Override
    public void executeTask() {
        // announce removal task
        System.out.printf("@%s: executing remove message task with message id %d for user %s%n", componentId, id, user);
        List<Message> messages = mails.get(user);
        // existence of message is assumed to be checked by task creator (MailboxDMAPHandler)
        messages.remove(id - 1);
    }
}
